package com.chen.config;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class DataSourceAop {
    @Pointcut(
            "!@annotation(com.chen.annotion.Master) "
                    + "&& (execution(* com.chen..*.service..*.select*(..)) " +
                    "|| execution(* com.chen..*.service..*.get*(..)))"
    )
    public void readPointcut() {
    }

    @Pointcut(
            "@annotation(com.chen.annotion.Master) " +
                    "|| execution(* com.chen..*.service..*.insert*(..))" +
                    "|| execution(* com.chen..*.service..*.add*(..))" +
                    "|| execution(* com.chen..*.service..*.save*(..))" +
                    "|| execution(* com.chen..*.service..*.update*(..))" +
                    "|| execution(* com.chen..*.service..*.edit*(..))" +
                    "|| execution(* com.chen..*.service..*.delete*(..))" +
                    "|| execution(* com.chen..*.service..*.remove*(..))"
    )
    public void writePointcut() {
    }

    @Before("readPointcut()")
    public void read() {
        DBContextHolder.slave();
    }

    @Before("writePointcut()")
    public void write() {
        DBContextHolder.master();
    }
}
